package com.sanploy.wechat.util;

import okhttp3.ResponseBody;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhu yingzhi on 2017/9/21.
 */
public class WechatRetrofitUtil {
//
//    private static final Retrofit retrofit = new Retrofit.Builder()
////            .baseUrl("https://api.weixin.qq.com/")
//            .build();
    public static String login(String appid,String code,String script) throws IOException {
        RetrofitService wechatServer = new Retrofit.Builder()
            .baseUrl("https://api.weixin.qq.com/")
                .build().create(RetrofitService.class);

        Call<ResponseBody> stringCall = wechatServer.userLogin(appid, script, code,"authorization_code");
        Response<ResponseBody> response = stringCall.execute();
        if (!response.isSuccessful()) System.out.println("不成功");
//        System.out.println(response.body().getList().size());
        return response.body().string();
    }

    public static String unifiedOrder(String xml) throws IOException {


//        RetrofitService wechatServer = new Retrofit.Builder()
//                .baseUrl("https://api.mch.weixin.qq.com")
//                .build().create(RetrofitService.class);
//        Call<ResponseBody> stringCall = wechatServer.unifiedOrder(xml);
//        Response<ResponseBody> response = stringCall.execute();
//        if (!response.isSuccessful()) System.out.println("不成功");
//        System.out.println(response.body().getList().size());
//        return response.body().string();
        return getPayNo("https://api.mch.weixin.qq.com/pay/unifiedorder",xml);
    }

    public String asd(){
        return "sad";
    }

    public static String getPayNo(String url, String xmlParam)
    {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        System.out.println("xml是:" + xmlParam);
        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter("http.protocol.allow-circular-redirects", Boolean.valueOf(true));
        HttpPost httpost = getPostMethod(url);
        String prepay_id = "";
        try {
            httpost.setEntity(new StringEntity(xmlParam, "UTF-8"));
            HttpResponse response = httpclient.execute(httpost);
            String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            Map dataMap = new HashMap();
            System.out.println("json是:" + jsonStr);
return jsonStr;
//            if (jsonStr.indexOf("FAIL") != -1) {
//                return prepay_id;
//            }
//            Map map = doXMLParse(jsonStr);
//            String return_code = (String)map.get("return_code");
//            prepay_id = (String)map.get("prepay_id");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return prepay_id;
    }

    public static HttpPost getPostMethod(String url)
    {
        HttpPost pmethod = new HttpPost(url);
        pmethod.addHeader("Connection", "keep-alive");
        pmethod.addHeader("Accept", "*/*");
        pmethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        pmethod.addHeader("Host", "api.mch.weixin.qq.com");
        pmethod.addHeader("X-Requested-With", "XMLHttpRequest");
        pmethod.addHeader("Cache-Control", "max-age=0");
        pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
        return pmethod;
    }
}
